﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace HY.DAL
{
	/// <summary>
	/// 数据访问类:S_Tree
	/// </summary>
	public partial class S_Tree
	{
		public S_Tree()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("NodeID", "S_Tree"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int NodeID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from S_Tree");
			strSql.Append(" where NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NodeID", SqlDbType.Int,4)			};
			parameters[0].Value = NodeID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(HY.Model.S_Tree model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into S_Tree(");
			strSql.Append("NodeID,Text,ParentID,ParentPath,Location,OrderID,comment,Url,PermissionID,ImageUrl,ModuleID,KeShiDM,KeshiPublic)");
			strSql.Append(" values (");
			strSql.Append("@NodeID,@Text,@ParentID,@ParentPath,@Location,@OrderID,@comment,@Url,@PermissionID,@ImageUrl,@ModuleID,@KeShiDM,@KeshiPublic)");
			SqlParameter[] parameters = {
					new SqlParameter("@NodeID", SqlDbType.Int,4),
					new SqlParameter("@Text", SqlDbType.VarChar,100),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@ParentPath", SqlDbType.VarChar,50),
					new SqlParameter("@Location", SqlDbType.VarChar,50),
					new SqlParameter("@OrderID", SqlDbType.Int,4),
					new SqlParameter("@comment", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,100),
					new SqlParameter("@PermissionID", SqlDbType.Int,4),
					new SqlParameter("@ImageUrl", SqlDbType.VarChar,100),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@KeShiDM", SqlDbType.Int,4),
					new SqlParameter("@KeshiPublic", SqlDbType.VarChar,10)};
			parameters[0].Value = model.NodeID;
			parameters[1].Value = model.Text;
			parameters[2].Value = model.ParentID;
			parameters[3].Value = model.ParentPath;
			parameters[4].Value = model.Location;
			parameters[5].Value = model.OrderID;
			parameters[6].Value = model.comment;
			parameters[7].Value = model.Url;
			parameters[8].Value = model.PermissionID;
			parameters[9].Value = model.ImageUrl;
			parameters[10].Value = model.ModuleID;
			parameters[11].Value = model.KeShiDM;
			parameters[12].Value = model.KeshiPublic;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HY.Model.S_Tree model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update S_Tree set ");
			strSql.Append("Text=@Text,");
			strSql.Append("ParentID=@ParentID,");
			strSql.Append("ParentPath=@ParentPath,");
			strSql.Append("Location=@Location,");
			strSql.Append("OrderID=@OrderID,");
			strSql.Append("comment=@comment,");
			strSql.Append("Url=@Url,");
			strSql.Append("PermissionID=@PermissionID,");
			strSql.Append("ImageUrl=@ImageUrl,");
			strSql.Append("ModuleID=@ModuleID,");
			strSql.Append("KeShiDM=@KeShiDM,");
			strSql.Append("KeshiPublic=@KeshiPublic");
			strSql.Append(" where NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@Text", SqlDbType.VarChar,100),
					new SqlParameter("@ParentID", SqlDbType.Int,4),
					new SqlParameter("@ParentPath", SqlDbType.VarChar,50),
					new SqlParameter("@Location", SqlDbType.VarChar,50),
					new SqlParameter("@OrderID", SqlDbType.Int,4),
					new SqlParameter("@comment", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,100),
					new SqlParameter("@PermissionID", SqlDbType.Int,4),
					new SqlParameter("@ImageUrl", SqlDbType.VarChar,100),
					new SqlParameter("@ModuleID", SqlDbType.Int,4),
					new SqlParameter("@KeShiDM", SqlDbType.Int,4),
					new SqlParameter("@KeshiPublic", SqlDbType.VarChar,10),
					new SqlParameter("@NodeID", SqlDbType.Int,4)};
			parameters[0].Value = model.Text;
			parameters[1].Value = model.ParentID;
			parameters[2].Value = model.ParentPath;
			parameters[3].Value = model.Location;
			parameters[4].Value = model.OrderID;
			parameters[5].Value = model.comment;
			parameters[6].Value = model.Url;
			parameters[7].Value = model.PermissionID;
			parameters[8].Value = model.ImageUrl;
			parameters[9].Value = model.ModuleID;
			parameters[10].Value = model.KeShiDM;
			parameters[11].Value = model.KeshiPublic;
			parameters[12].Value = model.NodeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int NodeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from S_Tree ");
			strSql.Append(" where NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NodeID", SqlDbType.Int,4)			};
			parameters[0].Value = NodeID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string NodeIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from S_Tree ");
			strSql.Append(" where NodeID in ("+NodeIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HY.Model.S_Tree GetModel(int NodeID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 NodeID,Text,ParentID,ParentPath,Location,OrderID,comment,Url,PermissionID,ImageUrl,ModuleID,KeShiDM,KeshiPublic from S_Tree ");
			strSql.Append(" where NodeID=@NodeID ");
			SqlParameter[] parameters = {
					new SqlParameter("@NodeID", SqlDbType.Int,4)			};
			parameters[0].Value = NodeID;

			HY.Model.S_Tree model=new HY.Model.S_Tree();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["NodeID"]!=null && ds.Tables[0].Rows[0]["NodeID"].ToString()!="")
				{
					model.NodeID=int.Parse(ds.Tables[0].Rows[0]["NodeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Text"]!=null && ds.Tables[0].Rows[0]["Text"].ToString()!="")
				{
					model.Text=ds.Tables[0].Rows[0]["Text"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ParentID"]!=null && ds.Tables[0].Rows[0]["ParentID"].ToString()!="")
				{
					model.ParentID=int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ParentPath"]!=null && ds.Tables[0].Rows[0]["ParentPath"].ToString()!="")
				{
					model.ParentPath=ds.Tables[0].Rows[0]["ParentPath"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Location"]!=null && ds.Tables[0].Rows[0]["Location"].ToString()!="")
				{
					model.Location=ds.Tables[0].Rows[0]["Location"].ToString();
				}
				if(ds.Tables[0].Rows[0]["OrderID"]!=null && ds.Tables[0].Rows[0]["OrderID"].ToString()!="")
				{
					model.OrderID=int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["comment"]!=null && ds.Tables[0].Rows[0]["comment"].ToString()!="")
				{
					model.comment=ds.Tables[0].Rows[0]["comment"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Url"]!=null && ds.Tables[0].Rows[0]["Url"].ToString()!="")
				{
					model.Url=ds.Tables[0].Rows[0]["Url"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PermissionID"]!=null && ds.Tables[0].Rows[0]["PermissionID"].ToString()!="")
				{
					model.PermissionID=int.Parse(ds.Tables[0].Rows[0]["PermissionID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ImageUrl"]!=null && ds.Tables[0].Rows[0]["ImageUrl"].ToString()!="")
				{
					model.ImageUrl=ds.Tables[0].Rows[0]["ImageUrl"].ToString();
				}
				if(ds.Tables[0].Rows[0]["ModuleID"]!=null && ds.Tables[0].Rows[0]["ModuleID"].ToString()!="")
				{
					model.ModuleID=int.Parse(ds.Tables[0].Rows[0]["ModuleID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["KeShiDM"]!=null && ds.Tables[0].Rows[0]["KeShiDM"].ToString()!="")
				{
					model.KeShiDM=int.Parse(ds.Tables[0].Rows[0]["KeShiDM"].ToString());
				}
				if(ds.Tables[0].Rows[0]["KeshiPublic"]!=null && ds.Tables[0].Rows[0]["KeshiPublic"].ToString()!="")
				{
					model.KeshiPublic=ds.Tables[0].Rows[0]["KeshiPublic"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select NodeID,Text,ParentID,ParentPath,Location,OrderID,comment,Url,PermissionID,ImageUrl,ModuleID,KeShiDM,KeshiPublic ");
			strSql.Append(" FROM S_Tree ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" NodeID,Text,ParentID,ParentPath,Location,OrderID,comment,Url,PermissionID,ImageUrl,ModuleID,KeShiDM,KeshiPublic ");
			strSql.Append(" FROM S_Tree ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM S_Tree ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.NodeID desc");
			}
			strSql.Append(")AS Row, T.*  from S_Tree T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "S_Tree";
			parameters[1].Value = "NodeID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

